import SwiftUI
import SwiftData

// AccountInfoPage
// 账号信息修改页面

struct AccountInfoPage: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var loginInfo: [LoginInfo]
    @Query private var userInfo: [UserInfo]
    
    @Environment(\.dismiss) private var dismiss

    @State private var nickNameInput: String = ""
    @State private var gradeInput: String = ""
    @State private var campusInput: String = ""
    @State private var collegeInput: String = ""
    @State private var contactInput: String = ""
    
    @StateObject private var loginMoodifyUserInfoRequester = LoginMoodifyUserInfoRequester()
    
    var body: some View {
        List {
            Section {
                HStack {
                    Text("昵称")
                    Spacer()
                    TextField("昵称", text: $nickNameInput)
                        .multilineTextAlignment(.trailing)
                }
            }
            
            Section {
                HStack {
                    Text("年级")
                    Spacer()
                    TextField("年级", text: $gradeInput)
                        .multilineTextAlignment(.trailing)
                        .keyboardType(.decimalPad)
                }
                
                HStack {
                    Text("校区")
                    Spacer()
                    TextField("校区", text: $campusInput)
                        .multilineTextAlignment(.trailing)
                }
                
                HStack {
                    Text("学院")
                    Spacer()
                    TextField("学院", text: $collegeInput)
                        .multilineTextAlignment(.trailing)
                }
            }
            
            Section {
                HStack {
                    Text("联系方式")
                    TextEditor(text: $contactInput)
                        .multilineTextAlignment(.trailing)
                }
            }
        }
        .navigationTitle("账号信息")
        .navigationBarTitleDisplayMode(.inline)
        .onAppear(perform: {
            if let lastuserInfo = userInfo.last {
                nickNameInput = lastuserInfo.nickName
                gradeInput = String(lastuserInfo.grade)
                campusInput = lastuserInfo.campus
                collegeInput = lastuserInfo.college
                contactInput = lastuserInfo.contact
            }
        })
        .toolbar {
            Button {
                loginMoodifyUserInfoRequester.request_basic(userId: loginInfo.last?.userId ?? "",
                                                            nickName: nickNameInput,
                                                            grade: gradeInput,
                                                            campus: campusInput,
                                                            college: collegeInput,
                                                            contact: contactInput)
            } label: {
                Text("完成")
            }
        }
        .onChange(of: loginMoodifyUserInfoRequester.requestStatus) { oldValue, newValue in
            if(oldValue == .requesting && newValue == .finish) {
                if(loginMoodifyUserInfoRequester.publishedResult) {
                    dismiss()
                }
            }
        }
    }
}

#Preview {
    AccountInfoPage()
}
